﻿using Microsoft.AspNetCore.SignalR;
using NanjingbeiRailwayStation.Core;
using NanjingbeiRailwayStation.Data.ClickHouse.Models;
using NanjingbeiRailwayStation.Data.ClickHouse.Services;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace NanjingbeiRailwayStation.WebApi.Hubs
{
    /// <summary>
    /// 
    /// </summary>
    public class MessageHub : Hub
    {

      
        /// <summary>
        /// 
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        public async Task BroadcastEventReport(string imei)
        {


            Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

             
            try
            {

                    var entityCache = await new Data.ClickHouse.Services.RealTimeElectricalParametersService().GetCurrentState(imei);

                await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, StringConstant.RealTime, Newtonsoft.Json.JsonConvert.SerializeObject(entityCache, jsonSerializerSettings));


                var todayElectricityConsumption
                    = await new RealTimeElectricalParametersService().GetTodayElectricityConsumption();

                if (todayElectricityConsumption != null)
                {
                    await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, StringConstant.TodayElectricityConsumption, Newtonsoft.Json.JsonConvert.SerializeObject(todayElectricityConsumption, jsonSerializerSettings ));

                }

                var todayAlarmFrequency
                   = await new EventReportService().GetTodayAlarmFrequency();

                if (todayAlarmFrequency != null)
                {
                    await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, StringConstant.TodayAlarmFrequency, Newtonsoft.Json.JsonConvert.SerializeObject(todayAlarmFrequency, jsonSerializerSettings));
                }

                var alarmTypeDistribution = await new EventReportService().GetAlarmTypeDistribution();

                if (alarmTypeDistribution != null)
                {
                    await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, StringConstant.AlarmTypeDistribution, Newtonsoft.Json.JsonConvert.SerializeObject(alarmTypeDistribution, jsonSerializerSettings     ));
                }

                var eventNotification = await new Data.ClickHouse.Services.EventReportService().GetLastEventNotification(imei);
                if (eventNotification != null){

                   await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, StringConstant.EventNotification, Newtonsoft.Json.JsonConvert.SerializeObject(eventNotification, jsonSerializerSettings));
                }

                } catch (Exception ex )
            {
                await Clients.Others.SendAsync(StringConstant.BroadcastEventReport, ex.Message+ "Exception");
            }
              
            await Task.CompletedTask;
         
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        public async Task BroadcastRealTimeElectricalParameters(string imei)
        {

            Newtonsoft.Json.JsonSerializerSettings jsonSerializerSettings = new JsonSerializerSettings()
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };


            var entityCache = await new Data.ClickHouse.Services.RealTimeElectricalParametersService().GetCurrentState(imei);
             

          await  Clients.Others.SendAsync(StringConstant.BroadcastRealTimeElectricalParameters, StringConstant.RealTime, Newtonsoft.Json.JsonConvert.SerializeObject(entityCache, jsonSerializerSettings));


            var todayElectricityConsumption
               = await new RealTimeElectricalParametersService().GetTodayElectricityConsumption();

            if (todayElectricityConsumption != null)
            {
                await Clients.Others.SendAsync(StringConstant.BroadcastRealTimeElectricalParameters, StringConstant.TodayElectricityConsumption, Newtonsoft.Json.JsonConvert.SerializeObject(todayElectricityConsumption, jsonSerializerSettings));

                 
                var todayCarbonEmission = new TodayCarbonEmission();

                todayCarbonEmission.CarbonEmissionL1 = todayElectricityConsumption.ActiveEnergyImportInPhaseL1;
                todayCarbonEmission.CarbonEmissionL2 = todayElectricityConsumption.ActiveEnergyImportInPhaseL2;
                todayCarbonEmission.CarbonEmissionL3 = todayElectricityConsumption.ActiveEnergyImportInPhaseL3;

                await Clients.Others.SendAsync(StringConstant.BroadcastRealTimeElectricalParameters, StringConstant.TodayCarbonEmission, Newtonsoft.Json.JsonConvert.SerializeObject(todayCarbonEmission, jsonSerializerSettings));

            }

            var todayLossRatio = await new RealTimeElectricalParametersService().GetTodayLossRatio();

            if (todayLossRatio != null)
            {
                await Clients.Others.SendAsync(StringConstant.BroadcastRealTimeElectricalParameters, StringConstant.TodayLossRatio, Newtonsoft.Json.JsonConvert.SerializeObject(todayLossRatio, jsonSerializerSettings));
            }

            var energyConsumptionRanking = await new RealTimeElectricalParametersService().GetEnergyConsumptionRanking(10);

            await Clients.Others.SendAsync(StringConstant.BroadcastRealTimeElectricalParameters, StringConstant.EnergyConsumptionRanking, Newtonsoft.Json.JsonConvert.SerializeObject(energyConsumptionRanking, jsonSerializerSettings      ));
             

           
            await Task.CompletedTask;
        }

         
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            await Clients.Caller.SendAsync("Echo", "OnConnectedAsync");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override Task OnDisconnectedAsync(Exception? exception)
        {
   
            return base.OnDisconnectedAsync(exception);
        }

    }
}
